<template name="home">
	<view>		
		<view class="cu-card article" v-for="(blog,index) in blogs" :key="index"  @tap="showDetail(blog)">
			<view class="cu-item shadow">
				<view class="title">
					<view class="text-cut">{{blog.title}}</view>
				</view>
				<view style="display: flex;flex-direction: row;justify-content: center">
					<image :src="blog.cover"
								 mode="widthFix" style="width: 100%"></image>
				</view>
				<view class="content">
					<view class="desc">
						<view class="text-content"> {{blog.desc}}</view>
					</view>
				</view>
			</view>
		</view>
		<view class="loading-text">{{loadingText}}</view>	

		<view class="cu-load load-modal" v-if="loadModal">
			<!-- <view class="cuIcon-emojifill text-orange"></view> -->
			<image src="/static/logo.png" mode="aspectFit" style="width: 150upx;height: 150upx;"></image>
		</view>

	</view>
</template>

<script>
	import { queryPosts } from '../../utils/service.js'
	import { formatBlogs } from "../../utils/format";

	export default {
		data() {
			return {
				loadModal:false,
				blogs:[],
				user:{},
				page:1,
				size:10,
				loadingText:"没有更多了"
			}
		},
		async onLoad() {
			this.page = 1;
			this.loadModal = true;
			this.blogs = await queryPosts(this.page,this.size)
			this.blogs  = formatBlogs(this.blogs)
			this.user = this.blogs[0].user
			uni.setStorageSync("user",this.user);
			this.loadModal = false;
		},
		methods: {
			showDetail(data){
				uni.navigateTo({
					url: '../detail/detail?id='+data.number
				})
			}
		},
		//上拉加载，需要自己在page.json文件中配置"onReachBottomDistance"
		async onReachBottom(){
			uni.showToast({title: '加载中'});
			this.page = this.page +1;
			this.loadingText="正在加载..."
			let res = await queryPosts(this.page,this.size)
			if(res.length == 0){
				this.loadingText="没有更多的了"
			}else{
				let blogs = formatBlogs(res)
				blogs.forEach(b => {
					this.blogs.push(b)
				})				
			}		
			console.log(this.blogs)
			
		}
	}
</script>

<style lang="scss">
.loading-text{
			width: 100%;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 60upx;
			color: #979797;
			font-size: 24upx;
		}
</style>
